package com.smartstate.ems.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartstate.ems.dto.PageDTO;
import com.smartstate.ems.entity.CarbonEmissions;
import com.smartstate.ems.service.EmissionsService;
import com.smartstate.ems.vo.ResultVO;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;

@RestController
@RequestMapping("/Emissions")
public class CarbonEmissionsController implements Serializable {

    @Resource
    private EmissionsService emissionsService;

    @PostMapping("/save")
    @ApiOperation(value = "新增资产")
    public ResultVO<Boolean> save(@RequestBody CarbonEmissions carbonEmissions ) {
        int integer = emissionsService.getCarbon(carbonEmissions);
        if (integer == 0){
            return ResultVO.fail("该月份碳排放已存在! 无法添加! 请前往更新!!!");
        }
        return ResultVO.ok(emissionsService.save(carbonEmissions));
    }

    @DeleteMapping("/removeById")
    @ApiOperation(value = "根据ID删除")
    public ResultVO<Boolean> removeById(@RequestParam int id) throws SchedulerException {
        return ResultVO.ok(emissionsService.removeAssetById(id));
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "修改资产")
    public ResultVO<Boolean> updateById(@RequestBody CarbonEmissions carbonEmissions ) {
        return ResultVO.ok(emissionsService.updateById(carbonEmissions));
    }

    @PostMapping("/pageAsset")
    @ApiOperation(value = "单表分页查询")
    public Page<CarbonEmissions> pageEquipment(@RequestBody PageDTO<CarbonEmissions> pageDTO) {
        return emissionsService.queryAllPageByDTO(pageDTO);
    }
}
